package com.white.hot.controller.jpush;

import java.util.Map;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;

/***
 * 极光的推送类， 官方推送有什么交集，并集之类的东西，不知道什么鬼，没有实现
 * 标签，别名以参数提供
 * @author Administrator
 *
 */
public class JPushManager
{

	private static JPushManager instance;
	private static JPushClient client;

	private JPushManager(String masterKey, String appKey)
	{
		client = new JPushClient(masterKey, appKey);
	}

	public static JPushManager getInstance(String masterKey, String appKey)
	{
		if (instance == null)
		{
			synchronized (JPushManager.class)
			{
				if (instance == null)
				{
					instance = new JPushManager(masterKey, appKey);
				}
			}
		}
		return instance;
	}

	/***
	 * 发送通知给安卓设备
	 * 
	 * @param title
	 *            通知的标题
	 * @param content
	 *            通知的内容
	 * @param alias
	 *            通知的alias，为空时就是广播，否则需要传入正确的alias
	 * @return
	 */
	public boolean sendAndroidBroadcast(String title, String content,
			String[] alias, String[] tags)
	{
		PushPayload payload = PushPayload
				.newBuilder()
				.setPlatform(Platform.android())
				.setAudience((alias.length == 0 && tags.length == 0) ? Audience.all() :
							Audience.newBuilder()
								.addAudienceTarget(AudienceTarget.tag(tags))
								.addAudienceTarget(AudienceTarget.alias(alias))
						.build())
				.setNotification(Notification.android(content, title, null))
				.build();
		try
		{
			PushResult result = client.sendPush(payload);
			System.out.println(result.isResultOK() + "--"
					+ result.getOriginalContent());
			return result.isResultOK();
		} catch (APIConnectionException e)
		{

		} catch (APIRequestException e)
		{
		}
		return false;

	}

	/***
	 * 携带额外信息的通知
	 * @param title 标题
	 * @param content 内容
	 * @param extra 额外的内容
	 * @param alias 别名
	 * @param tags 标签 
	 * @return
	 */
	public boolean sendAndroidExtraMessage(String title, String content,
			Map<String, String> extra, String[] alias, String[] tags)
	{
		PushPayload payload = PushPayload
				.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(
						Audience.newBuilder()
								.addAudienceTarget(
										AudienceTarget.alias(alias))
										.addAudienceTarget(AudienceTarget.tag(tags))
								.build())
				.setMessage(Message.newBuilder()
						.setMsgContent(content)
						.setTitle(title)
				.addExtras(extra).build()).build();
		try
		{
			PushResult result = client.sendPush(payload);
			System.out.println(result.isResultOK() + "--"
					+ result.getOriginalContent());
			return result.isResultOK();
		} catch (APIConnectionException e)
		{
			System.out.println(e.getMessage());
		} catch (APIRequestException e)
		{
			System.out.println(e.getMessage());
		}
		return false;
	}
}
